#include "stdafx.h"
#include "XamlTextReaderSettings.h"

extern XSTRING_BASE sDefaultNamespacePrefix;
extern XSTRING_BASE sDefaultNamespaceUri;


XamlTextReaderSettings::XamlTextReaderSettings()
{
	this->m_bRequireDefaultNamespace = FALSE;
	this->m_shouldProcessUid = FALSE;
}

HRESULT XamlTextReaderSettings::Create(XBOOL bRequireDefaultNamespace, XBOOL shouldProcessUid, xshared_ptr<XamlTextReaderSettings> & spReaderSettings)
{

	HRESULT hr = S_OK;


	XASSERT_SUCCESSED(spReaderSettings.init(new XamlTextReaderSettings()));

	spReaderSettings->m_bRequireDefaultNamespace = bRequireDefaultNamespace;
	spReaderSettings->m_shouldProcessUid = shouldProcessUid;

	return hr;

}

const XBOOL & XamlTextReaderSettings::get_ShouldProcessUid()
{

	return this->m_shouldProcessUid;

}

HRESULT XamlTextReaderSettings::get_RequireDefaultNamespace(XBOOL & fRequireDefaultNamespace)
{

	fRequireDefaultNamespace = this->m_bRequireDefaultNamespace;

	return S_OK;

}

HRESULT XamlTextReaderSettings::AddDefaultXmlNamespacePrefixesIfNotRequired(const xshared_ptr<XamlParserContext> & spParserContext) const
{
	HRESULT hr = S_OK;

	if(!this->m_bRequireDefaultNamespace)
	{
		xshared_ptr<CXString> spDefaultXmlNsPrefix;
		xshared_ptr<XamlNamespace> spDefaultXmlNamespace;

		XASSERT_SUCCESSED(spDefaultXmlNsPrefix.init(CXString(sDefaultNamespacePrefix).Clone(TRUE,FALSE)));

		hr = spParserContext->FindNamespaceByPrefix(spDefaultXmlNsPrefix,spDefaultXmlNamespace);

		if(hr == S_FALSE)
		{
			xshared_ptr<CXString> spDefaultXmlNsUri;
			xshared_ptr<XamlSchemaContext> spSchemaContext;
			hr = S_OK;

			XASSERT_SUCCESSED(spDefaultXmlNsUri.init(CXString(sDefaultNamespaceUri).Clone(TRUE,FALSE)));

			XASSERT_SUCCESSED(spParserContext->get_SchemaContext(spSchemaContext));

			XASSERT_SUCCESSED(spSchemaContext->GetXamlNamespace(spDefaultXmlNsUri, spDefaultXmlNamespace));

			XASSERT_SUCCESSED(spParserContext->AddNamespacePrefix(spDefaultXmlNsPrefix, spDefaultXmlNamespace));
		}
	}

	return hr;
}
